package com.xzy.seckill.common.validator;

import com.xzy.seckill.common.annotations.IsMobile;
import com.xzy.seckill.common.exception.Assert;
import com.xzy.seckill.utils.ValidatorUtils;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;


/**
 * 自定义校验规则类，用在自定义注解上
 */
public class IsMobileValidator implements ConstraintValidator<IsMobile,String > {


    private boolean required;

    //初始化操作
    @Override
    public void initialize(IsMobile constraintAnnotation) {
        required = constraintAnnotation.required();  //获取到此字段是否必填
    }

    //验证
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {

        System.out.println(value);
        if (required)  //必填
            {
            return ValidatorUtils.isMobile(value);
        }

           if (StringUtils.isEmpty(value))
               return true;



           return ValidatorUtils.isMobile(value);


    }
}
